package com.xhu.controller;

import com.xhu.entity.User;
import com.xhu.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@Slf4j
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private HttpSession session;

    @PostMapping("/login")
    public User login(@RequestBody User userInfo){
        //检查登录用户的账号和密码
        User user = userService.checkUserInfo(userInfo);
        //如果登录成功则将userInfo存储到session中
        if(user != null){
            session.setAttribute("userInfo", user);
        }

        log.debug("登录时候的sessionId: "+session.getId());

        return user;
    }

    @GetMapping("/logout")
    public boolean logOut(){
        session.removeAttribute("userInfo");

        return true;
    }
}
